//
//  IfengDocRelationCell.m
//  IfengNews
//
//  Created by Ryan on 13-8-14.
//
//

#import "IfengDocRelationCell.h"
#import "INHtmlElement.h"
#import "CLGloble.h"
#import "UIImageView+WebCache.h"

@interface IfengDocRelationCell ()
@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UIImageView *seprator;
@property (nonatomic,strong) UIImageView *iconImgView;
@property (nonatomic,strong) UIView *selectView;
@property (nonatomic,strong) UIImageView *titleImg;
@property (nonatomic,strong) UIButton *mark;

@property (nonatomic,strong) NSDictionary *dic;

@property (nonatomic, strong) UIImageView *commentCountIcon;
@property (nonatomic, strong) UILabel *commentCountLabel;

@end

@implementation IfengDocRelationCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self=[super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        
        // cell重用会自动高亮上次选中的，解不了，只能恢复到以前没有点击效果的状态了
       // self.selectionStyle = UITableViewCellSelectionStyleDefault;
        
        _selectView = [[UIView alloc] init];
       // _selectView.backgroundColor = ICurrentTheme.docStyle.releativeSelectBg;
        _selectView.backgroundColor=[UIColor colorWithWhite:(float)0xd6/0xff alpha:1.0];
     
        _selectView.frame = self.bounds;
        self.selectedBackgroundView = _selectView;
//        self.selectView = _selectView;
     
        _seprator=[[UIImageView alloc] init];
        //_seprator.image=[UIImage imageNamed:ICurrentTheme.cellStyle.indexCellSeperator];
        _seprator.image=[UIImage imageNamed:@"seperator_line.png"];
      
        [self.contentView addSubview:_seprator];
        
        self.titleLabel=[[UILabel alloc] init];
        _titleLabel.backgroundColor = [UIColor clearColor];
       
        self.titleLabel.textAlignment = NSTextAlignmentLeft;
        self.titleLabel.numberOfLines = 0;
        self.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        //self.titleLabel.textColor = ICurrentTheme.docStyle.releativeTitleColor;
        self.titleLabel.textColor=RGB(0x22, 0x22, 0x22);
      
        self.titleLabel.font = [UIFont systemFontOfSize:17];
        self.titleLabel.autoresizingMask =UIViewAutoresizingFlexibleHeight;
        
        self.titleLabel.frame=CGRectMake(12+80+12, 12.f, 204, self.titleLabel.font.lineHeight*2+2);
       
        [self.contentView addSubview:self.titleLabel];
        
        self.titleImg=[[UIImageView alloc] init];
        self.titleImg.frame=CGRectMake(12, 12.0f, 80.0f, 59.0f);
        //self.titleImg.image=[UIImage imageNamed:ICurrentTheme.docStyle.docRelationImg];
//        self.titleImg.image=[UIImage imageNamed:@"doc_relative.png"];
      
        
        [self addSubview:self.titleImg];
        
        _commentCountIcon = [[UIImageView alloc]initWithFrame:CGRectMake(GlobleWidth-47.f, 60.f, 12.f, 12.f)];
       
        _commentCountIcon.hidden = YES;
        _commentCountIcon.image = [UIImage imageNamed:@"index_commentCount_icon.png"];
        [self addSubview:_commentCountIcon];
        
        _commentCountLabel = [[UILabel alloc]initWithFrame:CGRectMake(_commentCountIcon.frame.origin.x+_commentCountIcon.frame.size.width+4.f, 61.f, 40.f, 10.f)];

        _commentCountLabel.hidden = YES;
        _commentCountLabel.textAlignment = NSTextAlignmentLeft;
        _commentCountLabel.font = [UIFont fontWithName:@"Avenir-Book" size:10];
        _commentCountLabel.textColor=RGB(0x5b, 0x85, 0x9e);
        [self.contentView addSubview:_commentCountLabel];
        
        
        self.dic = [[NSDictionary alloc]init];

    }
    return self;
}

- (void)docDic:(NSDictionary *)dic{
    self.dic = dic;
    [self.titleImg sd_setImageWithURL:[NSURL URLWithString:[dic sgrGetStringForKey:@"titlepic"]]
                             placeholderImage:[UIImage imageNamed:@"index_thumbnail.png"]];
    
    int com=[dic sgrGetIntForKey:@"comments"];
    [self setComment:com];
    
}

- (void)setComment:(int)comment {
    
    if (comment && comment>0) {
        _commentCountIcon.hidden = NO;
        _commentCountLabel.hidden = NO;
        [_commentCountLabel setText:[NSString stringWithFormat:@"%d",comment]];
    }else{
        _commentCountIcon.hidden = YES;
        _commentCountLabel.hidden = YES;
    }
    
}

- (void)setIsRead:(BOOL)isRead{
//    self.titleLabel.textColor=isRead?[UIColor grayColor]:RGB(0x22, 0x22, 0x22);
//    self.titleImg.image=isRead?[UIImage imageNamed:@"doc_relative_open.png"]:[UIImage imageNamed:@"doc_relative.png"];
}

- (void)doSelect:(BOOL)isSelect{
    if(isSelect){
        self.titleLabel.backgroundColor=self.selectView.backgroundColor;
    }else{
        self.titleLabel.backgroundColor=[UIColor clearColor];
    }
}

- (void)setTitle:(NSString *)title isLastCell:(BOOL)isLastCell{
    // 分割线
    _seprator.hidden = isLastCell;
//    _seprator.frame = CGRectMake(0, [[self class] caculate:title]-1, GlobleWidth, 1.0f);
    _seprator.frame = CGRectMake(0, 82-0.5, GlobleWidth, 0.5f);
    
    // 相关新闻标题
    
    self.titleLabel.text = title;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    //CGFloat labelHeight = [[self class] caculate:self.titleLabel.text];
//    CGFloat labelHeight = 68;
    CGSize size=[self.titleLabel.text sizeWithFont:[UIFont systemFontOfSize:17]];
    if(size.width>198){
    _titleLabel.frame = CGRectMake(12+80+12, 12.f, 204, _titleLabel.font.lineHeight*2+2);
    }else{
        _titleLabel.frame = CGRectMake(12+80+12, 12.f, 204, _titleLabel.font.lineHeight+1);
    }
    // 修复高亮背景在两行标题下偏上（高度小）的BUG
    _selectView.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), 82);
}

- (void)setICONWithType:(NSString *)type{
    if (!_iconImgView) {
        _iconImgView = [[UIImageView alloc] init];
        _iconImgView.backgroundColor = [UIColor redColor];
    }
    NSArray *types = [type componentsSeparatedByString:@"__"];
    NSString *iconType = [types lastObject];
    if ([@"video" isEqualToString:[iconType lowercaseString]]) {
        NSLog(@"视频");
    }else if([@"slide" isEqualToString:[iconType lowercaseString]]){
        NSLog(@"图集");
    }
    
}

// 这是改bug17875，原因不明，table莫名其妙记录上次点击cell，重用时会自动设置cell选中并高亮显示！！
- (void)setSelected:(BOOL)selected{
    [super setSelected:NO];
}


+ (float)caculate:(NSString *)title{
    if(_isStrNotNull(title)){
        CGSize size=[title sizeWithFont:[UIFont systemFontOfSize:17]];
        return size.width>(I_DOC_CONTENT_WIDTH-16.0f)?55.0f:38.0f;
    }else{
      return 0.0f;  
    }
    
}

- (void)setLastMark{
    if(!self.mark){
        self.mark=[UIButton buttonWithType:UIButtonTypeCustom];
        self.mark.frame=CGRectMake(0, 69, GlobleWidth, 25.0f);
        [self addSubview:self.mark];
    }
    
}

- (void)closeLastMark{
    if(self.mark){
        [self.mark removeFromSuperview];
        self.mark=nil;
    }
}





@end
